package com.cwt_taxi_xa.util;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.cwt_taxi_xa.MainApplication;

import android.annotation.SuppressLint;
import android.content.Intent;

/**
 * 时间帮助类
 *
 * @author sj
 *
 */
@SuppressLint("SimpleDateFormat")
public class DateUtil {

	private static final String TAG = "DateUtil.";
	@SuppressLint("SimpleDateFormat")
	public static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmm");
	public static SimpleDateFormat df20 = new SimpleDateFormat("yyyyMMddHH");
	static SimpleDateFormat df3 = new SimpleDateFormat("MM月dd日");
	static SimpleDateFormat df4 = new SimpleDateFormat("yyyy年MM月");
	static SimpleDateFormat df5 = new SimpleDateFormat("HH:mm");
	public static SimpleDateFormat df6 = new SimpleDateFormat("yyyyMMddHHmmss");
	public static SimpleDateFormat df7 = new SimpleDateFormat("MM月dd日 HH:mm");
	public static SimpleDateFormat df8 = new SimpleDateFormat("yyMMddHHmmss");
	public static SimpleDateFormat df9 = new SimpleDateFormat("yyyyMMdd");
	public static SimpleDateFormat df10 = new SimpleDateFormat("MMddHHmmss");
	public static SimpleDateFormat df11 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static SimpleDateFormat df12 = new SimpleDateFormat("HH:mm:ss");
	public static SimpleDateFormat df13 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static SimpleDateFormat df14 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
	public static SimpleDateFormat df19 = new SimpleDateFormat("HHmmss");

	/**
	 * 格式化日期
	 * @param format
	 * @return
	 */
	public static String dateFormatForTimePicker(String time) {
		Date date = null;
		String str = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-d H:m").parse(time);
			str = df11.format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 通过时间获取毫秒值
	 * @param timeStr	yyyy-MM-dd HH:mm:ss
	 * @return	millis	135465556545
	 */
	public static long getTimeMillis(SimpleDateFormat sdf, String timeStr){
		long millis = 0;
		try {
			Date date = sdf.parse(timeStr);
			millis = date.getTime();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return millis;
	}

	/**
	 * GPS实际那转换成年月日时间
	 * @param gpstime
	 * @return
	 */
	public static String gpsTimeToTime(long gpstime,String format){
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(gpstime);
			SimpleDateFormat df=new SimpleDateFormat(format);
			String datestring = df.format(calendar.getTime());
			return datestring;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * GPS实际那转换成年月日时间
	 * @param gpstime
	 * @return
	 */
	public static String gpsTimeToTime(long gpstime,SimpleDateFormat df){
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(gpstime);
			String datestring = df.format(calendar.getTime());
			return datestring;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取当天日期
	 *
	 * yyyy-MM-dd
	 */
	public static String getTodayDate() {
		return df.format(new Date());
	}

	public static String getTodayDateDf9() {
		return df9.format(new Date());
	}
	
	public static String getTimeDf19() {
		return df19.format(new Date());
	}
	//判断是否校时
	public static boolean in20Century() {
		int year = Integer.parseInt(df.format(new Date()).substring(0, 4));
		return year>2000&&year<3000;
	}

	/**
	 * 获取当前时间
	 *
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static String getNowDate() {
		return df1.format(new Date());
	}

	/**
	 * 获取当前时间，计价器所需刷卡时间
	 *
	 * yyyyMMddHHmm
	 */
	public static String getNowDateToMeter() {
		return df2.format(new Date());
	}

	public static String getNowDateToMeterSetting() {
		return df20.format(new Date());
	}

	/**
	 * 获取前第七天
	 */
	public static String getLastSevenDay() {

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -15);

		return df.format(cal.getTime());
	}

	/**
	 * 获取当前日期的上一天
	 */
	public static String getMinusDate(String date) {
		try {
			Date tempDate = df.parse(date);
			Calendar cal = Calendar.getInstance();
			cal.setTime(tempDate);
			cal.add(Calendar.DATE, -1);
			return df.format(cal.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前日期的下一天
	 */
	public static String getPlusDate(String date) {
		try {
			Date tempDate = df.parse(date);
			Calendar cal = Calendar.getInstance();
			cal.setTime(tempDate);
			cal.add(Calendar.DATE, +1);
			return df1.format(cal.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 格式化日期
	 * @param format
	 * @return
	 */
	public static String dateFormat(String format) {
		Date date = null;
		String str = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-d H:m:s").parse(format);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			str = sdf.format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 格式时间
	 * 150528134521  转变为 2015-05-28 13:45:21
	 * @param 	150528134521
	 * @return	2015-05-28 13:45:21
	 */
	public static String dateTimeFormat(String datetime){
		String str = null;
		try {
			StringBuffer strBuffer = new StringBuffer();
			strBuffer.append("20");
			strBuffer.append(datetime.substring(0, 2));
			strBuffer.append("-");
			strBuffer.append(datetime.substring(2, 4));
			strBuffer.append("-");
			strBuffer.append(datetime.substring(4, 6));
			strBuffer.append(" ");
			strBuffer.append(datetime.substring(6, 8));
			strBuffer.append(":");
			strBuffer.append(datetime.substring(8, 10));
			strBuffer.append(":");
			strBuffer.append(datetime.substring(10, 12));

			str = strBuffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 计算两个日期相差多少天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long timeDifference(String sdate,String edate){
		long day = 0;
		try {
			SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date begin=dfs.parse(sdate);
			Date end = dfs.parse(edate);
			long between=(end.getTime()-begin.getTime())/1000;   //除以1000是为了转换成秒
			day=between/(24*3600);
		} catch (Exception e) {

			e.printStackTrace();
		}
		return day;

	}

	/**
	 * 日期时间比较
	 * @param DATE1
	 * @param DATE2
	 * @return DATE1 > DATE2 返回1
	 */
	public static int compare_date(String DATE1, String DATE2) {
		try {
			Date dt1 = df1.parse(DATE1);
			Date dt2 = df1.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception e) {

			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 日期比较
	 * @param DATE1
	 * @param DATE2
	 * @return DATE1 > DATE2 返回	1
	 * 		   DATE1 == DATE2 返回	0
	 * 		   DATE1 < DATE2 返回	-1
	 */
	public static int date_compare(String DATE1, String DATE2) {
		try {
			Date dt1 = df9.parse(DATE1);
			Date dt2 = df9.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception e) {

			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 判断date是否处于date1和date2之间
	 * @param date1
	 * @param date2
	 * @param date
	 * @return
	 */
	public static Boolean isBetweenDate1AndDate2(String date1,String date2,Date date){
		try {
			Date dt1 = df1.parse(date1);
			Date dt2 = df1.parse(date2);

			//当date1小于date2的时候
			if(dt1.getTime()<dt2.getTime()){
				if(date.getTime()>dt1.getTime()&&date.getTime()<dt2.getTime()){
					return true;
				}else{
					return false;
				}
				//当date1大于date2的时候
			}else if(dt1.getTime()>dt2.getTime()){
				if(date.getTime()>dt2.getTime()&&date.getTime()<dt1.getTime()){
					return true;
				}else{
					return false;
				}
			}else{
				//当date1等于date2的时候
				if(date.getTime()==dt1.getTime()){
					return true;
				}else{
					return false;
				}
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return false;
		}
	}

	/**
	 * 判断date是否处于date1和date2之间
	 * @param date1
	 * @param date2
	 * @param date
	 * @return
	 */
	public static Boolean isBetweenDate1AndDate2(SimpleDateFormat sdf,String date1,String date2,String date){
		try {
			Date dt1 = sdf.parse(date1);
			Date dt2 = sdf.parse(date2);
			Date dt3 = sdf.parse(date);

			//当date1小于date2的时候
			if(dt1.getTime()<dt2.getTime()){
				if(dt3.getTime()>dt1.getTime()&&dt3.getTime()<dt2.getTime()){
					return true;
				}else{
					return false;
				}
				//当date1大于date2的时候
			}else if(dt1.getTime()>dt2.getTime()){
				if(dt3.getTime()>dt2.getTime()&&dt3.getTime()<dt1.getTime()){
					return true;
				}else{
					return false;
				}
			}else{
				//当date1等于date2的时候
				if(dt3.getTime()==dt1.getTime()){
					return true;
				}else{
					return false;
				}
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return false;
		}
	}



	/**
	 * 获取按要求的当天日期时间
	 *
	 */
	public static String getTodayDate(SimpleDateFormat dateFormat) {
		return dateFormat.format(new Date());
	}

	/** 把 time 从 sdf1 格式转变为 sdf2 格式*/
	public static String getTime(
			String time,SimpleDateFormat sdf1,SimpleDateFormat sdf2){
		try {
			Date date = sdf1.parse(time);
			time = sdf2.format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}



	/**
	 * 判断是否需要更新系统时间
	 * @return
	 */
	/*private static boolean needUpdateTime(){
		boolean updateTime = false;
		// 获取系统当前时间
		String systemdate = df1.format(Calendar.getInstance().getTime());
		long currentTime = System.currentTimeMillis();
		// 获取记忆时间
		long lastTime = SPutil.defaultPref.getLong("last_time", System.currentTimeMillis());
		// 时间差
		long diffTime= currentTime - lastTime;
		if(diffTime < 120 || systemdate.startsWith("2013")){
			updateTime = true;
		}
		AppLog.i(TAG, "系统当前时间："+systemdate+"; diffTime: "+diffTime);
		return updateTime;
	}*/

	/**
	 * 日期优化工具
	 * @param time	yyyy-MM-dd HH:mm:ss
	 * @return	优化后的显示时间
	 */
	public static String getShowTime(String time){
		try {
			String showTime = "";
			if(isThisYear(time)){	// 今年
				if(isToday(time)){		// 今天
					showTime = parseHourAndMinute(time);
				}else if(isYesterday(time)){	// 昨天
					showTime = "昨天";
				}else{	// 其他
					showTime = parseMonthAndDay(time);
				}
			}else{	// 不是今年
				showTime = df4.format(df1.parse(time));
			}
			return showTime;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 预约订单时间优化
	 * @param time
	 * @return	当天：HH:mm
	 * 			非当天：MM月dd日 HH:mm
	 */
	public static String getOrderTime(String time){
		try {
			if(isToday(time)){
				return parseHourAndMinute(time);
			}else{
				Date d = df1.parse(time);
				return df7.format(d);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 判断是否今年
	 * @param compareTime yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static boolean isThisYear(String compareTime){
		try {
			Calendar calendar = Calendar.getInstance();
			int thisYear = calendar.get(Calendar.YEAR);

			Date d = df1.parse(compareTime);
			calendar.setTime(d);
			int compareYear = calendar.get(Calendar.YEAR);

			return thisYear == compareYear;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 判断是否今天
	 * @param compareTime 要判断的时间
	 * @return
	 */
	public static boolean isToday(String compareTime){
		String today = getTodayDate();
		String compare = getDateFromString(compareTime);
		return today.equals(compare);
	}

	/**
	 * 判断是否昨天
	 * @param compareTime 要判断的时间
	 * @return
	 */
	public static boolean isYesterday(String compareTime){
		String yesterday = getYestoday();
		String compare = getDateFromString(compareTime);
		return yesterday.equals(compare);
	}

	/**
	 * 截取日期
	 * MM-dd
	 */
	public static String parseMonthAndDay(String time) {
		try {
			Date d = df1.parse(time);
			return df3.format(d);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 截取时分
	 * HH:mm
	 */
	public static String parseHourAndMinute(String time) {
		try {
			Date d = df1.parse(time);
			return df5.format(d);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 把字符串解析成
	 * yyyy-MM-dd
	 * @param text
	 */
	public static String getDateFromString(String time){
		try {
			Date d = df1.parse(time);
			String result = df.format(d);
			return result;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			System.out.println("time="+time);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取昨天
	 * yyyy-MM-dd
	 */
	public static String getYestoday() {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			cal.add(Calendar.DATE, -1);
			return df.format(cal.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 预约订单结束时间差
	 * @param time
	 * @return
	 */
	public static String getTimeLeft(String time){
		try {
			String timeText = "";
			long timeMs = df1.parse(time).getTime();
			long currentTime = System.currentTimeMillis();
			/** 单位：分钟*/
			long timeBewteen = (timeMs - currentTime)/1000/60;
			if(timeBewteen < 0 || timeBewteen == 0){
				timeText = "已到期";
			}else if(timeBewteen < 60){
				timeText = "剩余"+timeBewteen+"分钟";
			}else if(timeBewteen < 60 * 24){
				timeText = "剩余"+(timeBewteen/60)+"小时"+(timeBewteen%60)+"分钟";
			}else{
				timeText = "剩余"+(timeBewteen/(60*24))+"天"+((timeBewteen%(60*24))/60)+"小时"+(timeBewteen%(60*24)%60)+"分钟";
			}
			//			timeText = ""+timeBewteen+"分钟";
			AppLog.i("计算预约订单剩余时间："+timeText);
			return timeText;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			AppLog.i("计算预约订单剩余时间出错");
			return "错误时间";
		}
	}



	/**
	 * 当前时间与记忆时间的差，00:00格式
	 * @param time
	 */
	public static int time_sec;
	public static String getShowTime2(long time){
		time_sec = (int) (System.currentTimeMillis()-time);
		return sec2Time((time_sec/1000));
//		if(time/60>9&&time%60>9){
//			return time/60+"."+time%60;
//		}
//		else if(time/60>9&&time%60<9){
//			return time/60+".0"+time%60;
//		}
//		else if(time/60<9&&time%60>9){
//			return "0"+time/60+"."+time%60;
//		}
//		else{
//			return "0"+time/60+".0"+time%60;
//		}

	}

	/**
	 * 秒转时间00:00:00
	 * @param sec
	 * @return
	 */
	public static String sec2Time(int sec){
		String timeText = "";
		try {
			/** 单位：分钟*/
			if(sec <= 0){
				timeText = "00:00:00";
			}else if(sec < 60){
				String compltSec = String.valueOf(sec);
				if(sec < 10){
					compltSec = "0"+String.valueOf(sec);
				}
				timeText = "00:00:"+compltSec;
			}else if(sec < 60 * 60){
				int min = sec/60;
				String compltMin = String.valueOf(min);
				if(min < 10){
					compltMin = "0"+String.valueOf(min);
				}
				int sec2 = sec%60;
				String compltSec = String.valueOf(sec2);
				if(sec2 < 10){
					compltSec = "0"+String.valueOf(sec2);
				}
				timeText = "00:"+compltMin+":"+compltSec;
			}else{
				int hour = sec/(60*60);
				String compltHour = String.valueOf(hour);
				if(hour < 10){
					compltHour = "0"+String.valueOf(hour);
				}
				int min = sec%3600/60;
				String compltMin = String.valueOf(min);
				if(min < 10){
					compltMin = "0"+String.valueOf(min);
				}
				int sec2 = sec%60%60;
				String compltSec = String.valueOf(sec2);
				if(sec2 < 10){
					compltSec = "0"+String.valueOf(sec2);
				}
				timeText = compltHour+":"+compltMin+":"+compltSec;
			}
			return timeText;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "00:00:00";
		}
	}
}
